package cn.andy.android.crm.fragment;

import java.util.List;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import cn.andy.android.crm.R;
import cn.andy.android.crm.adapter.MyAdapter;
import cn.andy.android.crm.beans.PersonInfo;
import cn.andy.android.crm.db.PersonDBHelper;

public class SQLiteDemoFragment extends Fragment{
	
	private PersonDBHelper helper;
	private View view;
	private ListView listview;
	private MyAdapter adapter;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		view = inflater.inflate(R.layout.fragment_sqlitedemo, container, false);
		
		helper = new PersonDBHelper(getActivity());
		
		loadData2ListView();
		
		initComp();
		
		return view;
	}
	
	private void loadData2ListView(){
		Cursor cursor = helper.select();
		List<PersonInfo> persons = helper.get(cursor);
		
		adapter = new MyAdapter(getActivity(), persons, helper);
		listview = (ListView) view.findViewById(R.id.sqlitedemo_listview);
		listview.setAdapter(adapter);
		
		listview.setOnItemClickListener(clickListener);
	}
	
	OnItemClickListener clickListener = new AdapterView.OnItemClickListener() {

		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			adapter.selectedpos = position;
			adapter.notifyDataSetInvalidated();
			PersonInfo p = adapter.arrays.get(position);
			
			loadPerson(p);
			
		}
			
	};
	
	private void initComp(){
		Button btnAdd = (Button) view.findViewById(R.id.add);
		btnAdd.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View view) {
				LayoutInflater factory = LayoutInflater.from(getActivity());
				View editview = factory.inflate(R.layout.person_edit_ui, null);
				new PersonEditUI(getActivity(), editview, null, helper){
					@Override
					public void afterDataUpdate() {

						Cursor cursor = helper.select();
						List<PersonInfo> persons = helper.get(cursor);
						adapter.arrays = persons;
						adapter.notifyDataSetChanged();
					}
				};
			}
		});
		
		Button btnHelp = (Button) view.findViewById(R.id.help);
		btnHelp.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View view) {
				Toast.makeText(getActivity(), R.string.tab_sqlitedemo_help, Toast.LENGTH_LONG).show();
			}
		});

	}

	private void loadPerson(PersonInfo p){
		EditText etname = (EditText) view.findViewById(R.id.sqlitedemo_name);
		EditText etemail = (EditText) view.findViewById(R.id.sqlitedemo_email);
		EditText etaddress = (EditText) view.findViewById(R.id.sqlitedemo_address);
			
		etname.setText(p.getName());
		etemail.setText(p.getEmail());
		etaddress.setText(p.getAddress());
	}
	
}
